package com.five.taotao.guide_classify;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.five.taotao.Adapter.GridAdapter;
import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.dao.ProductListBeanDao;
import com.five.taotao.entity.Product;
import com.five.taotao.entity.ProductList;
import com.five.taotao.http.CallBack;
import com.five.taotao.product_details.ProductDetailsActivity;
import com.five.taotao.search.SearchResultActivity;
import com.five.taotao.selfwidget.CustomProgressDialog;
import com.five.taotao.util.Constants;
import com.five.taotao.util.VolleyUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class RightFragment extends Fragment {

    @BindView(R.id.top_text)
    TextView top_text;
    @BindView(R.id.myGridView)
    GridView myGridView;

    @OnClick(R.id.top_text)
    public void search(View v) {
        Intent intent = new Intent(getActivity(), SearchResultActivity.class);
        startActivity(intent);
    }
    
    private View v;
    private GridAdapter adapter;
    private List<Product.ListBean> mData;
    private int productType;//商品的分类

    private ProductListBeanDao dao;
    private ProgressDialog myDialog;
    CustomProgressDialog progressDialog;

    public RightFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (v == null) {
            v = inflater.inflate(R.layout.fragment_right, container, false);
            ButterKnife.bind(this,v);
            dao = new ProductListBeanDao(getActivity());

            mData = new ArrayList<>();
            mData.addAll(dao.findAllProduct());
            adapter = new GridAdapter(mData,getActivity());
            myGridView.setAdapter(adapter);

            top_text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), SearchResultActivity.class);
                    intent.putExtra("productType", productType);
                    startActivity(intent);
                }
            });

            myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
                    intent.putExtra("product",mData.get(position));
                    startActivity(intent);
                }
            });

        }
        return v;
    }


    /**
     * 更新对应产品信息
     * @param productList
     */
    public void refresh(ProductList productList) {
        productType = productList.getId();
        top_text.setText(productList.getName() + " 专区 >>");

        if (productType == 0) {
            mData.clear();
            mData.addAll(dao.findAllProduct());
            adapter.notifyDataSetChanged();
        } else {
            circle();

            VolleyUtil.Post(Constants.C_URL +"?id="+productType+"&page=1")
                    .setCallBack(new MyCallBack())
                    .build()
                    .setPriority(Request.Priority.HIGH)
                    .start();
        }


    }


    /**
     * 网络监听回调方法
     */
    private class MyCallBack implements CallBack {

        @Override
        public void onSuccess(String response) {
            Gson gson = new Gson();
            Product musicWebAlbum = gson.fromJson(response, Product.class);
            List<Product.ListBean> items = musicWebAlbum.getList();
            mData.clear();
            mData.addAll(items);
            adapter.notifyDataSetChanged();
            myDialog.dismiss();
            //progressDialog.dismiss();
        }

        @Override
        public void onErrer(VolleyError error) {
            MyApplication.toast.toastShow("网络异常，请检查网络.");
        }

    }


    /**
     * 圆形进度条测试..
     */
    public void circle() {
        myDialog = new CustomProgressDialog(getActivity(),R.style.dialog);
        Window wd= myDialog.getWindow();
        WindowManager.LayoutParams lp = wd.getAttributes();
        lp.alpha = 0.9f;
        wd.setAttributes(lp);
        myDialog.show(); // 显示进度条
    }

}
